<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class Journal extends Model
{
    use HasDateTimeFormatter;
    protected $table = 'journal';

    protected $fillable =
    [
        'name',
        'issn_eissn',
        'type',
        'jcr_abbreviation',
        'total_citations',

        'jif_2022',
        'jif_quartile',
        'jci_2022',
        'oa_gold',
        'sjr',
        'h_index',
        'cite_score',
        'snip',
        'public_cycle',
        'type_label',
        'open_access',
        'public_difficult',
        'publisher',
        'country_area',
        'acceptance_time',
        'website',
    ];


    /**
     * 获取期刊列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-05-08
     * @param [type] $payload
     * @return void
     */
    public static function getJournalList($payload)
    {

        $where = function ($query) use ($payload) {

            if (isset($payload['name']) && $payload['name'] != '') {
                $query->where('name', 'like', '%' . $payload['name'] . '%');
            }

            if (isset($payload['issn_eissn']) && $payload['issn_eissn'] != '') {
                $query->where('issn_eissn',  $payload['name']);
            }
            if (isset($payload['type']) && $payload['type'] != '') {
                $query->where('type', $payload['type']);
            }
            if (isset($payload['open_access']) && $payload['open_access'] != '') {
                $query->where('open_access', $payload['open_access']);
            }
            if ((isset($payload['jif_2022_min']) && $payload['jif_2022_min'] != '') && isset($payload['jif_2022_max']) && $payload['jif_2022_max'] != '') {
                $query->whereBetween('jif_2022', [$payload['jif_2022_min'], $payload['jif_2022_max']]);
            }
        };

        return self::where($where)
            ->select('id', 'name', 'type', 'jif_2022', 'h_index', 'oa_gold', 'public_cycle', 'open_access', 'public_difficult', 'publisher', 'acceptance_time', 'created_at')
            ->orderBy('created_at', 'desc')
            ->paginate($payload['limit']);
    }


    /**
     * 获取期刊详情
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-05-09
     * @param [type] $id
     * @return void
     */
    public static function getJournalDetail($id)
    {
        $detail = self::find($id);
        $data['detail'] = $detail;
        $data['recommen'] = self::where('type', 'like', '%' . $detail->type . '%')
            ->select('id', 'name', 'type', 'jif_2022', 'h_index', 'oa_gold', 'public_cycle', 'open_access', 'public_difficult', 'publisher', 'acceptance_time', 'created_at')
            ->orderBy('created_at', 'desc')
            ->take(5)
            ->get();
        return $data;


        /**
         * yixinyuan.admin
         * www.gongshicloud.com
         * test.tuotou.api.medcase.com.cn
         * test.tofund.high-med.com
         * test.public.netloop.cn
         * test.gongshicloud.com
         * score.netloop.cn
         * roche.admin.medcase.com.cn
         * news.gongshicloud.com
         * meet.netloop.cn
         * medical.netloop.cn
         * medical.gongshicloud.com
         * lc.2024.admin.medcase.com.cn
         * laravel.gongshicloud.com
         * jyqj.api.medcase.com.cn
         * innovent-medical.netloop.cn
         * hr.medsky.cn
         * gg.capstone.org.cn
         * divorceweb.gongshicloud.com
         * discuz.netloop.cn
         */
    }
}
